$(function () {
    if (document.cookie) {
        if (getcookie("lgc")) {
            $(".show").css({ display: "none" }).removeClass()
            $(".hide").css({ display: "block" })
            $(".hide a").eq(0).html(`欢迎你:${getcookie("lgc")}`)
        }
    }


    $(".a1").on("click", function () {
        deletecookie("lgc")
        location.href = "./index.html"
    })


    $(".bannerleft").hover(function () {
        $(".bannerbuttom").css({ display: "block" })
    }, function () {
        $(".bannerbuttom").css({ display: "none" })
    })

    $(".i1").click(function () {
        $(".foot").animate({ height: 0 }, 2000, function () {
            $(".foot").css({ opacity: 0, height: 0 })
            $(".i1").css({ display: "none" })
            $(".i2").css({ display: "block" })
        })
    })
    $(".i2").click(function () {
        $(".foot").animate({ height: "100%", opacity: 1 }, 2000, function () {
            $(".i1").css({ display: "block" })
            $(".i2").css({ display: "none" })
        })
    })

    // 商品的动态生成   =>  模板字符串 => 可以根据既定的模板批量生成

    var field = "id";//页面加载时  默认按照id排序
    var ordertype = "asc";//页面加载时  默认升序排序
    var page = 1;//页面加载时  默认显示第一页
    var shownum = 10;//页面加载时  默认每页显示5条

    yemian();

    $(".search_bar").click(event => {
        var event = event || window.event;
        var target = event.target || event.srcElement;
        if (target.className == "orderCol") {
            field = target.value;
            yemian();
        }
        else if (target.className == "orderType") {
            ordertype = target.value;
            yemian();
        }
        else if (target.className == "resetBtn") {
            key = "";
            field = "id";
            ordertype = "asc";
            page = 1;
            shownum = 10;
            yemian();
        }
    })


    $(".select").change(function () {
        console.log(this);
        shownum = this.value;
        yemian();
    })

    async function yemian() {
        var url = urlParse();
        var { key } = url;
        var result = await goodsziduan({ key, field, ordertype, page, shownum })
        var { status, msg, sql, currentpage, maxpage } = result;
        if (status) {
            if (sql.length) {
                var html = "";
                sql.forEach(({ goodsId, goodsName, goodsImg, goodsPrice }) => {
                    html += `<li class="more2_item more2_item_good hover-on">
             <a href="../html/gooddetail.html?gid=${goodsId}"> 
             <div class="o2_impr"></div>
              <a class="more2_lk" href="../html/gooddetail2.html?gid=${goodsId}"
                  target="_blank" title="${goodsName}" aria-label="${goodsName}"
                  clstag="h|keycount|feed|01#pd_049-a">
                  <div class="mod_lazyload_placeholder" style="height: 220px;">
                      <img
                          src="https:${goodsImg}"
                          class="lazyimg_img"
                          alt="${goodsName}">   
                  </div>
                  <div class="more2_info">
                      <p class="more2_info_name"><i class="more2_info_self">自营</i>${goodsName}</p>
                      <div class="more2_info_price more2_info_price_plus more2_info_price_newcomer">
                          <div class="mod_price"><i>¥</i><span class="more2_info_price_txt">${String(goodsPrice).split(".")[0]}.<span
                                      class="more2_info_price_txt-decimal">${String(goodsPrice).split(".")[1] == undefined ? 0 : String(goodsPrice).split(".")[1]}</span></span></div>
                      </div>
                  </div>
                  <div class="more2_item_hover">
                  <div class="more2_item_delete" clstag="h|keycount||feed|01#pd_049-b" title="不喜欢"></div>
                  <div class="more2_item_hd" title="">
                      <div class="more2_item_btn more2_btn_find enable" clstag="h|keycount||feed|01#pd_049-c">
                          <i class="more2_btn_icon"></i><span>找相似</span></div>
                  </div>
              </div>
              </a>
              </a>
          </li>`
                })
                $(".more2_list").html(html);
                page = page > maxpage ? maxpage : page;
                $(".pageTips").text(`${currentpage}/${maxpage}`)

                // 事件监听绑定=>给一个元素绑定多个事件
                $(".prev").off()
                $(".next").off()

                $(".next").on("click", function () {
                    if (page >= maxpage) return false
                    page++;
                    yemian();
                })

                $(".prev").on("click", function () {
                    if (page <= 1) return false
                    page--;
                    yemian();
                })


            } else {
                showGrade.innerText = msg
            }
        } else {
            alert(msg)
        }
    }


    $(document).on("click", ".shoptocar", async function () {
        location.href = "./shopcar.html"
    })

    $(".searchBtn").click(function () {
        if ($(".searchCon").val()) {
            location.href = `./goodslist.html?key=${$(".searchCon").val()}`
        }
    })

    $(document).on("click", ".login", async function () {
        var yonghu = getcookie("lgc")
        console.log(yonghu);
        if (!yonghu) {
            location.href = "./login_account.html?returnrul=" + encodeURIComponent(location.href)
        }
    })

})

function urlParse() {
    var search = location.search;
    var data = {};
    if (search) {  //从注册页跳转过来  => 有数据
        var str = search.substring(1);
        var list = str.split("&");
        // console.log(list);
        list.forEach(function (item) {
            var key = item.split("=")[0];
            var val = item.split("=")[1];
            data[key] = decodeURI(val);
        })
    }
    return data;
}